import axios from '@/axios.config'

export default {
    state: {
        msg: '',
        flag: 0,
        imList:[],
        totalNum:0
    },
    mutations: {
        send_msg(state, payload) {
            state.msg = payload._msg,
            state.flag = payload._flag
        },
        add_imlist(state, payload) {
            state.imList = [].concat(payload.com_im_list);
            state.totalNum = payload.com_totalNum
        }
    },
    actions: {
        //创建即时通讯
        createImCom({ commit }, payload) {
            return axios.post('/api/im/create', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "新建通讯接口成功",
                            _flag: 0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag: 1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag: 1
                    })
                })
        },
        //更新即时通讯
        updateImCom({ commit }, payload) {
            return axios.post('/api/im/update', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "更新通讯接口成功",
                            _flag: 0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag: 1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag: 1
                    })
                })
        },
        //即时通讯列表
        getImList({ commit }, payload) {
            return axios.post('/api/im/list', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_imlist',{
                            com_im_list: res.data.data.t,
                            com_totalNum: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //更新即时通讯状态
        updateImStatus({ commit }, payload) {
            return axios.post('/api/im/status', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "状态更新成功"
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //删除即时通讯
        deleteIm({ commit }, payload) {
            return axios.post('/api/im/del', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "删除成功"
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
    }
}